home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic 4 Database How-To / Visual Basic 4 Database - How-to (The Waite Group)(1995).iso / attach.fr_ / attach.fr
Text File  |  1995-02-12  |  7KB  |  226 lines

  1. VERSION 4.00
  2. Begin VB.Form frmAttach 
  3.    BackColor       =   &H00C0C0C0&
  4.    Caption         =   "Attachment Manager"
  5.    ClientHeight    =   2910
  6.    ClientLeft      =   1095
  7.    ClientTop       =   1515
  8.    ClientWidth     =   4200
  9.    BeginProperty Font 
  10.       name            =   "MS Sans Serif"
  11.       charset         =   1
  12.       weight          =   700
  13.       size            =   8.25
  14.       underline       =   0   'False
  15.       italic          =   0   'False
  16.       strikethrough   =   0   'False
  17.    EndProperty
  18.    Height          =   3315
  19.    Left            =   1035
  20.    LinkTopic       =   "Form1"
  21.    ScaleHeight     =   2910
  22.    ScaleWidth      =   4200
  23.    Top             =   1170
  24.    Width           =   4320
  25.    Begin VB.CommandButton cmdClose 
  26.       Caption         =   "&Close"
  27.       Height          =   555
  28.       Left            =   1200
  29.       TabIndex        =   2
  30.       Top             =   1860
  31.       Width           =   1815
  32.    End
  33.    Begin VB.CommandButton cmdDetach 
  34.       Caption         =   "&Detach a Table"
  35.       Height          =   555
  36.       Left            =   1200
  37.       TabIndex        =   1
  38.       Top             =   1080
  39.       Width           =   1815
  40.    End
  41.    Begin VB.CommandButton cmdAttach 
  42.       Caption         =   "&Attach a Table"
  43.       Height          =   555
  44.       Left            =   1200
  45.       TabIndex        =   0
  46.       Top             =   300
  47.       Width           =   1815
  48.    End
  49.    Begin MSComDlg.CommonDialog cmDlgFile 
  50.       Left            =   180
  51.       Top             =   1920
  52.       _version        =   65536
  53.       _extentx        =   847
  54.       _extenty        =   847
  55.       _stockprops     =   0
  56.    End
  57. End
  58. Attribute VB_Name = "frmAttach"
  59. Attribute VB_Creatable = False
  60. Attribute VB_Exposed = False
  61. Option Explicit
  62.  
  63. Const SOURCE_FILE = 1
  64. Const DESTINATION_FILE = 2
  65. Const DETACH_FILE = 3
  66.  
  67. Private Sub cmdAttach_Click()
  68.  
  69.     ' Get the user's choice of the file to which the table
  70.     ' is to be attached
  71.     GetMDBFile DESTINATION_FILE
  72.     
  73.     ' If the user chose a destination file, get the source of the
  74.     ' table from the user
  75.     
  76.     If DestinationFile.Name <> "" Then GetMDBFile SOURCE_FILE
  77.     
  78.     If SourceFile.Name <> "" Then
  79.     
  80.         ' The user designated a destination file and a source file.
  81.         ' Use frmSelector to get the user's selection of the source
  82.         ' file table to be attached to the destination file.
  83.         frmSelector.Tag = "Attach"
  84.         frmSelector.Show 1
  85.         
  86.         ' The user designated a table, so attach it.
  87.         If TableName <> "" Then AttachTable
  88.         
  89.     End If
  90.     
  91. End Sub
  92. Private Sub cmdDetach_Click()
  93.  
  94.     
  95.     ' Get the user's choice of the file from which the table
  96.     ' is to be detached
  97.     GetMDBFile DETACH_FILE
  98.     
  99.     If DestinationFile.Name <> "" Then
  100.     
  101.         ' The user designated a file.
  102.         ' Use frmSelector to get the user's selection of the source
  103.         ' file table to be detached from the file.
  104.         frmSelector.Tag = "Detach"
  105.         frmSelector.Show 1
  106.         
  107.         ' The user designated a table, so detach it.
  108.         If TableName <> "" Then DetachTable
  109.     End If
  110. End Sub
  111. Private Sub cmdClose_Click()
  112.     End
  113. End Sub
  114. Private Function ExtractPath(fileName As String, fullPath As String)
  115.  
  116.     ' Extract the pathname from the fully quialified filename and
  117.     ' return the pathname to the calling routine.
  118.     ExtractPath = Left$(fullPath, Len(fullPath) - (Len(fileName) + 1))
  119. End Function
  120. Private Sub DetachTable()
  121.     Dim db As Database
  122.     
  123.     ' Set the error handler.
  124.     On Error GoTo DetachTableError
  125.     
  126.     ' Open the database specified in the Public variable DestinationFile.
  127.     Set db = DBEngine.Workspaces(0).OpenDatabase(DestinationFile.Path _
  128.         & "\" & DestinationFile.Name)
  129.         
  130.     ' Delete the table definition designated by the Public variable
  131.     ' TableName.
  132.     db.TableDefs.Delete TableName
  133.     MsgBox "Table " & TableName & " detached from " & DestinationFile.Name
  134.     
  135. Exit Sub
  136.  
  137. DetachTableError:
  138.     MsgBox Error(Err)
  139. Exit Sub
  140.  
  141. End Sub
  142. Private Sub AttachTable()
  143.     Dim db As Database
  144.     Dim tblDef As TableDef
  145.     Dim dotPos As Integer
  146.     
  147.     ' Set the error handler.
  148.     On Error GoTo AttachFileError
  149.     
  150.     ' Open the database specified in the Public variable DestinationFile.
  151.     Set db = DBEngine.Workspaces(0).OpenDatabase(DestinationFile.Path _
  152.         & "\" & DestinationFile.Name)
  153.         
  154.     ' Create the table definition and set its properties. The table name
  155.     ' is contained in the Public variable SourceFile.
  156.     Set tblDef = db.CreateTableDef(TableName)
  157.     tblDef.Connect = ";DATABASE=" & SourceFile.Path & "\" & SourceFile.Name
  158.     tblDef.SourceTableName = TableName
  159.     
  160.     ' Add the table definition to the destination file's TableDefs
  161.     ' collection.
  162.     db.TableDefs.Append tblDef
  163.     MsgBox "Table " & TableName & " attached to " & DestinationFile.Name
  164.     
  165. Exit Sub
  166.  
  167. AttachFileError:
  168.     MsgBox Error(Err)
  169. Exit Sub
  170.  
  171. End Sub
  172. Private Sub GetMDBFile(purpose As Integer)
  173.  
  174.     ' Set the error handler.
  175.     On Error GoTo GetMDBFileError
  176.  
  177.     ' Set the caption property for the File Open common dialog.
  178.     Select Case purpose
  179.         Case SOURCE_FILE
  180.             cmDlgFile.DialogTitle = "Select Source File Of Table"
  181.         Case DESTINATION_FILE
  182.             cmDlgFile.DialogTitle = "Select File To Attach Table To"
  183.         Case DETACH_FILE
  184.             cmDlgFile.DialogTitle = "Select File To Detach Table From"
  185.     End Select
  186.     
  187.     ' Set the remaining properties for the File Open common dialog.
  188.     ' The dialog will facilitate selection of an existing MS Access file.
  189.     cmDlgFile.DefaultExt = "*.MDB"
  190.     cmDlgFile.Filter = "Access Files *.MDB|*.MDB|All Files *.*|*.*"
  191.     cmDlgFile.Flags = &H1000    ' OFN_FILEMUSTEXIST
  192.     cmDlgFile.CancelError = True
  193.     cmDlgFile.fileName = "*.MDB"
  194.     
  195.     ' Open the dialog box and get the user's selection.
  196.     cmDlgFile.ShowOpen
  197.     
  198.     ' From the user's selection, update the Name and Path members of
  199.     ' the appropriate Public variable.
  200.     If purpose = SOURCE_FILE Then
  201.         SourceFile.Name = cmDlgFile.FileTitle
  202.         SourceFile.Path = ExtractPath(cmDlgFile.FileTitle, _
  203.             cmDlgFile.fileName)
  204.     Else
  205.         DestinationFile.Name = cmDlgFile.FileTitle
  206.         DestinationFile.Path = ExtractPath(cmDlgFile.FileTitle, _
  207.             cmDlgFile.fileName)
  208.     End If
  209. Exit Sub
  210.  
  211. GetMDBFileError:
  212.  
  213.     ' The user clicked Cancel in the File Open dialog box. Set the members
  214.     ' of the appropriate Public variable to empty strings.
  215.     If purpose = SOURCE_FILE Then
  216.         SourceFile.Name = ""
  217.         SourceFile.Path = ""
  218.     Else
  219.         DestinationFile.Name = ""
  220.         DestinationFile.Path = ""
  221.     End If
  222. Exit Sub
  223.  
  224. End Sub
  225.  
  226.